package cn.brilliantfuture.commentry.store.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.entity.ExpressPerson;
import cn.brilliantfuture.commentry.store.ExpressCodesMessageWebService;
import cn.brilliantfuture.commentry.util.DateTimeHelper;

/**
 * 
 * Title: Communication Entry
 * 
 * Author:  LiTian  
 *
 * Date: 2014-2-24
 * 
 * Description:
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.store.ExpressCodesMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/ExpressCodesMessageWebService")
public class ExpressCodesMessageImpl implements ExpressCodesMessageWebService {
	
	private static Logger logger = Logger.getLogger(ExpressCodesMessageImpl.class);
	
	@PersistenceContext
	EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public String getMaxExpressPersonIds(int expressPersonId) {
		logger.info("expressPersonId -> " + expressPersonId);
		
		List<ExpressPerson> expressPersons = entityManager.createQuery("select ep from ExpressPerson ep where ep.deleted = false and ep.expressPersonId > " + expressPersonId + " order by ep.expressPersonId asc").setMaxResults(10).getResultList();
		
		JSONArray expressPersonIds = new JSONArray();
		JSONArray expressCodes = new JSONArray();
		
		for (ExpressPerson expressPerson : expressPersons) {
			expressPersonIds.add(expressPerson.getExpressPersonId());
			expressCodes.add(expressPerson.getExpressCode());
		}
		
		JSONObject result = new JSONObject();
		result.accumulate("expressPersonIds", expressPersonIds);
		result.accumulate("expressCodes", expressCodes);
		
		return result.toString();
	}

	@SuppressWarnings("unchecked")
	public String getRemovePersonIds(int lastTime) {
		logger.info("lastTime -> " + lastTime);

		JSONObject result = new JSONObject();
		
		StringBuffer sql = new StringBuffer();
		sql.append("select ep from ExpressPerson ep where ep.deleted = true");
		
		if (lastTime > 0) {
			sql.append(" and ep.deleteTime > '").append(DateTimeHelper.dateTimeToStr(new Date(lastTime * 1000L), DateTimeHelper.DEFAULT_DATE_TIME_FORMAT)).append("'");
		}
		
		sql.append(" order by ep.deleteTime asc");
		
		List<ExpressPerson> expressPersons = entityManager.createQuery(sql.toString()).setMaxResults(10).getResultList();
		
		if (expressPersons.size() > 0) {
			JSONArray expressPersonIds = new JSONArray();

			for (ExpressPerson expressPerson : expressPersons) {
				expressPersonIds.add(expressPerson.getExpressPersonId());
			}
			
			result.accumulate("expressPersonIds", expressPersonIds);
			result.accumulate("lastTime", expressPersons.get(expressPersons.size() - 1).getDeleteTime().getTime() / 1000);
		}

		return result.toString();
	}

}
